package websockets

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/gorilla/websocket"
	"gopkg.in/fatih/set.v0"
	"net/http"
)

type Node struct {
	userId int64
	Conn *websocket.Conn
	DataQueue chan []byte
	GroupSets set.Interface
}

//var (
//	// ClientMap 连接资源
//	ClientMap map[int64]*Node = make(map[int64]*Node, 0)
//	// RWLock 读写锁
//	RWLock sync.RWMutex
//
//)

func Doing(c *gin.Context)  {
	var (
		//token string
		isAllow bool
		err error
	)

	//token = c.GetHeader("token")
	//if token == "" {
	//	c.JSON(http.StatusOK, gin.H{
	//		"message":"缺少token",
	//	})
	//	return
	//}

	//是否允许连接
	isAllow = true
	//升级请求为websocket协议
	conn,err :=(&websocket.Upgrader{
		CheckOrigin: func(r *http.Request) bool {
			return isAllow
		},
	}).Upgrade(c.Writer, c.Request,nil)

	if err != nil {
		fmt.Println(err)
		c.JSON(http.StatusOK, gin.H{
			"message":"请使用websocket协议连接",
			"err": err.Error(),
		})
		return
	}

	//预关闭连接资源
	defer conn.Close()

	for  {
		mt, message, err := conn.ReadMessage()
		fmt.Println(string(message))
		if err != nil {
			fmt.Println(err)
			break
		}

		err = conn.WriteMessage(mt, message)
		if err != nil {
			fmt.Println(err)
			break
		}
	}
}


